package hackbard.piratradar;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Configuration extends Activity {
	public static final String PREFS_NAME = "PiratRadar";
	public static final int MENUE_SAVE = Menu.FIRST;
	public static final int MENUE_CANCEL = Menu.FIRST + 1;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.configuration);
		loadConfig();
		Button saveButton = (Button) findViewById(R.id.ButtonSaveConfig);
		saveButton.setOnClickListener(SaveOnClick);
	}

	private OnClickListener SaveOnClick = new OnClickListener() {
		public void onClick(View v) {
			saveConfig();
		}
	};

	private void saveConfig() {
		EditText EditTextGpsUpdate = (EditText) findViewById(R.id.EditTextGpsUpdate);
		EditText EditTextNick = (EditText) findViewById(R.id.EditTextNick);
		ToggleButton GpsAutoUpdater = (ToggleButton) findViewById(R.id.gpsautoupdater);
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		String gpsupdate = EditTextGpsUpdate.getText().toString();
		{
			if(settings.getLong("gpsupdate", 0) != (Long.valueOf((gpsupdate))))
			{
				Log.w("config:", "update interval changed");
				
				
			}
			
		editor.putLong("gpsupdate", (Long.valueOf((gpsupdate))));
		}
		editor.putString("nick", EditTextNick.getText().toString());
		if (GpsAutoUpdater.getText().toString().equals("An")) {
			editor.putBoolean("gpsautoupdater", true);
		} else {
			editor.putBoolean("gpsautoupdater", false);
		}
		editor.commit();
		Toast.makeText(getBaseContext(), "Gespeichert!", Toast.LENGTH_SHORT)
				.show();
	}

	private void loadConfig() {
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		EditText EditTextGpsUpdate = (EditText) findViewById(R.id.EditTextGpsUpdate);
		EditText EditTextNick = (EditText) findViewById(R.id.EditTextNick);
		ToggleButton GpsAutoUpdater = (ToggleButton) findViewById(R.id.gpsautoupdater);
		EditTextGpsUpdate.setText(String.valueOf(settings.getLong("gpsupdate",0)));
		EditTextNick.setText(settings.getString("nick", "anonymus"));
		GpsAutoUpdater.setChecked(settings.getBoolean("gpsautoupdater", true));
	}

}
